package com.rybbaby.tss.api.feedBack;

import com.rybbaby.tss.core.utils.AjaxJsonResponse;
import com.rybbaby.tss.core.utils.AppResponse;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssFeedback.entity.TssFeedback;
import com.rybbaby.tss.front.tssFeedback.service.ITssFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;


@RestController
@RequestMapping(value = "api/feedBack", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class FeedBackApi extends BaseController {

    @Autowired
    private ITssFeedbackService tssFeedbackService;

    /**
     * 接口默认返回
     *
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index() {
        return new AppResponse("hello ,welcome to R.Y.B common service API", 200).jsonResponse(false);
    }

    /**
     * 提交反馈
     *
     * @param entity
     *            被编辑对象
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "daily", method = RequestMethod.POST)
    public String deily(TssFeedback entity,@RequestParam(required = true)String token) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        String adminUserId = super.getLoginUser().getId();
        if(StringUtils.isNotBlank(token)){
            try {
                ajr.setResMsg("保存成功!");
                entity.setCreateUserId(adminUserId);
                entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
                entity.setUpdateTIme(new java.sql.Timestamp(System.currentTimeMillis()));
                Serializable saveId = tssFeedbackService.saveFeedback(entity);
                if(saveId == null){
                    ajr.setResMsg("保存失败!");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ajr.setResCode(500);
                ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
            }
        }else {
            ajr.setResCode(301);
            ajr.setResMsg("token不能为空");
        }
        return ajr.jsonResponse(false);
    }

}
